<template>
  <div>
    <div class="topNav">
      <van-nav-bar
        title="我的收藏"
        left-text="返回"
        left-arrow
        @click-left="quit"
      />
    </div>
    <div class="bgimg">
      <img src="../assets/images/bg1.png" alt="" />
    </div>
    <div class="content">
      <div class="list" v-for="(item, index) in list" :key="index" >
        <div class="img" @click="go(item.pid)"><img :src="item.smallImg" /></div>
        <div class="name">{{ item.name }}</div>
        <div class="enname">{{ item.enname }}</div>
        <div class="price">
          &yen;{{ item.price }}
          <van-icon name="delete-o" @click="notLike(item.pid)" />
        </div>
      </div>
    </div>
    <div v-if="list.length === 0">
      <van-empty image="error" description="没有喜欢的商品哦，快去选购吧">
      </van-empty>
    </div>
  </div>
</template>

<script>
export default {
  name: "Like",
  data() {
    return {
      list: [],
    };
  },
  methods: {
    quit() {
      this.$router.go(-1);
    },
    // 查找用户所有收藏的商品
    searchLike() {
      let token = this.$cookies.get("TOKEN");
      this.axios({
        method: "GET",
        url: "/findAllLike",
        params: {
          tokenString: token,
        },
      })
        .then((res) => {
          
          if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast("请先登录");
            setTimeout(() => {
              this.$router.push({ name: "Login" });
            }, 2000);
            return;
          }
          if (res.data.code === 2000) {
            this.list = res.data.result;
          }
        })
        .catch((err) => {
          
        });
    },
    // 取消收藏
    notLike(pid) {
      let token = this.$cookies.get("TOKEN");
      this.axios({
        method: "POST",
        url: "/notlike",
        data: {
          pid,
          tokenString: token,
        },
      })
        .then((res) => {
          if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast("请先登录");
            setTimeout(() => {
              this.$router.push({ name: "Login" });
            }, 2000);
            return;
          }
          if (res.data.code === 900 && res.data.result === 1) {
            //取消收藏商品
            this.$toast(res.data.msg);
          }
          this.searchLike();
        })
        .catch((err) => {
          
        });
    },
    // 跳转商品详情
    go(item){
      this.$router.push({name:"Goods",query:{item}})
    }
  },
  created() {
    this.searchLike();
  },
};
</script>

<style lang="less" scoped>
.topNav {
  position: sticky;
  top: 0;
  /deep/ .van-nav-bar__text {
    color: #0c34ba;
  }
  /deep/ .van-nav-bar .van-icon {
    color: #0c34ba;
  }
}
.bgimg {
  width: 100%;
  img {
    width: 100%;
  }
}
.content {
  background-color: white;
  position: fixed;
  top: 120px;
  left: 0;
  right: 0;
  margin: 10px;
  border-top-right-radius: 15px;
  border-top-left-radius: 15px;
  padding: 10px;
  box-sizing: border-box;
  padding-top: 20px;
  .list {
    display: inline-block;
    margin-right: 10px;
    margin-bottom: 10px;
    // width: 100px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    .img {
      width: 100px;
      height: 100px;
      img {
        width: 100%;
      }
    }
    .name {
      padding: 10px 0;
      font-size: 14px;
    }
    .enname {
      padding: 0px 0 10px 0;
      width: 100px;
      color: gray;
      font-size: 12px;
      text-overflow: ellipsis;
    }
    .price {
      color: #0c34ba;
      font-size: 14px;
      font-weight: bold;
    }
    /deep/ .van-icon {
      margin-left: 35px;
    }
  }
}
.custom-image .van-empty__image {
  width: 90px;
  height: 90px;
}
</style>